package com.example.demo.domain;


import com.example.demo.enums.ClauseCombine;
import com.example.demo.enums.Operator;
import com.example.demo.enums.ParameterType;
import com.example.demo.service.Clause;
import lombok.Data;

import java.util.Optional;
import java.util.function.Function;

@Data
public class SingleClause implements Clause {
    private String name;
    private Object[] values;
    private Operator operator;
    private ParameterType parameterType;

    public SingleClause(String name, Object... values) {
        this(name, Operator.Equal, values);
    }

    public SingleClause(String name, Operator operator, Object... values) {
        this.name = name;
        this.operator = operator;
        this.values = values;
        this.parameterType = ParameterType.Value;
    }

    public Object getValue() {
        return this.getValues().length > 0 ? this.getValues()[0] : null;
    }

    public Object getStartValue() {
        return this.getValues().length > 0 ? this.getValues()[0] : null;
    }

    public Object getEndValue() {
        return this.getValues().length > 1 ? this.getValues()[1] : null;
    }

    public CombineClause toCombine() {
        return this.toCombine(ClauseCombine.And);
    }

    public CombineClause toCombine(ClauseCombine combine) {
        CombineClause clause = new CombineClause(combine);
        clause.add(this);
        return clause;
    }

    public static SingleClause equal(String name, Object value) {
        return new SingleClause(name, Operator.Equal, new Object[]{value});
    }

    public static SingleClause notEqual(String name, Object value) {
        return new SingleClause(name, Operator.NotEqual, new Object[]{value});
    }

    public static SingleClause like(String name, String value) {
        return new SingleClause(name, Operator.Like, new Object[]{value});
    }

    public static SingleClause notLike(String name, String value) {
        return new SingleClause(name, Operator.NotLike, new Object[]{value});
    }

    public static SingleClause in(String name, Object... values) {
        return new SingleClause(name, Operator.In, values);
    }

    public static SingleClause notIn(String name, Object... values) {
        return new SingleClause(name, Operator.NotIn, values);
    }

    public static SingleClause greaterThan(String name, Object value) {
        return new SingleClause(name, Operator.GreaterThan, new Object[]{value});
    }

    public static SingleClause greaterThanEqual(String name, Object value) {
        return new SingleClause(name, Operator.GreaterThanEqual, new Object[]{value});
    }

    public static SingleClause lessThan(String name, Object value) {
        return new SingleClause(name, Operator.LessThan, new Object[]{value});
    }

    public static SingleClause lessThanEqual(String name, Object value) {
        return new SingleClause(name, Operator.LessThanEqual, new Object[]{value});
    }

    public static SingleClause between(String name, Object start, Object end) {
        return new SingleClause(name, Operator.Between, new Object[]{start, end});
    }

    public static SingleClause isNull(String name) {
        return new SingleClause(name, Operator.Null, new Object[]{true});
    }

    public static SingleClause notNull(String name) {
        return new SingleClause(name, Operator.NotNull, new Object[]{true});
    }

}